<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\admin\validate\AdminGroupValidate;
use app\admin\validate\BaseValidate;
use app\domain\admin\AdminGroupDomain;
use think\Exception;
use think\response\Json;

class AdminGroup extends Common
{
    public function retrieve(AdminGroupDomain $domain): Json
    {
        try {
            $params = request()->param();
            $validate = validate(BaseValidate::class);
            if (!$validate->scene('retrieve')->check($params)) {
                throw new \Exception($validate->getError());
            }

            $result = $domain->retrieve($params['page'], $params['pageSize'], $params['search'] ?? []);
            return jsonReturn(true, lang($domain->errMsg), $result);
        } catch (\Exception $e) {
            return jsonReturn(false, lang($e->getMessage()));
        }
    }

    public function create(AdminGroupDomain $domain)
    {
        try {
            $params = request()->param();
            $validate = validate(AdminGroupValidate::class);
            if (!$validate->scene('create')->check($params)) {
                throw new Exception($validate->getError());
            }

            $result = $domain->create($params['title'], $params['description'] ?? '', $params['menu_ids'], $params['status'] ?? 'enable');
            return jsonReturn($result, lang($domain->errMsg), $result, $domain->statusCode);
        } catch (\Exception $e) {
            return jsonReturn(false, lang($e->getMessage()));
        }
    }

    public function update(AdminGroupDomain $domain): Json
    {
        try {
            $params = request()->param();
            $validate = validate(AdminGroupValidate::class);
            if (!$validate->scene('create')->check($params)) {
                throw new Exception($validate->getError());
            }

            $result = $domain->update($params['id'], $params['title'], $params['description'] ?? '', $params['menu_ids'], $params['status'] ?? 'enable');
            return jsonReturn((bool)$result, lang($domain->errMsg), $result, $domain->statusCode);
        } catch (\Exception $e) {
            return jsonReturn(false, lang($e->getMessage()));
        }
    }

    public function delete(AdminGroupDomain $domain): Json
    {
        try {
            $params = request()->param();
            $validate = validate(BaseValidate::class);
            if (!$validate->scene('delete')->check($params)) {
                throw new \Exception($validate->getError());
            }

            $result = $domain->delete($params['id']);
            return jsonReturn($result, lang($domain->errMsg), $result);
        } catch (\Exception $e) {
            return jsonReturn(false, lang($e->getMessage()));
        }
    }
}
